package my.cn.bdqn.life;

import java.util.ArrayList;
import java.util.List;

import my.cn.bdqn.life.data.DataManager;
import my.cn.bdqn.life.db.DBHelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class CollectionActivity extends Activity {
	public static CollectionActivity current=null;
	private TextView headtext;
	private ListView savelist;
	private List<DataManager.SaveInfo> data;
	public static List<DataManager.Movie>ms=new ArrayList();
	private BaseAdapter adapter = new BaseAdapter() {

		public int getCount() {
			// TODO Auto-generated method stub
			return data.size();
		}

		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return data.get(arg0);
		}

		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		public View getView(int pos, View view, ViewGroup arg2) {
			// TODO Auto-generated method stub
			view = getLayoutInflater()
			.inflate(R.layout.collection_content,
					null);
			TextView tv = (TextView) view
			.findViewById(R.id.collection_name);
			tv.setText(data.get(pos).getContent());
			return view;
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.collection_layout);
		current=this;
		headtext = (TextView) findViewById(R.id.headtext);
		headtext.setText(R.string.save);
		savelist = (ListView) findViewById(R.id.savelist);
		refresh();
		savelist.setOnItemClickListener(new ListView.OnItemClickListener(){

			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				// TODO Auto-generated method stub
				
				lookup(pos);
			}});
	}
	public void lookup(int pos){
		ms.clear();
		DBHelper dbHelper=new DBHelper(this);
		ms.addAll(dbHelper.getSavedMovies());
		DataManager.SaveInfo s=data.get(pos);
		DataManager.Movie m=dbHelper.getSavedMovieByMid(s.getYid());
		dbHelper.close();
		int position=ms.indexOf(m);
		Intent intent=new Intent(this,DetailMovie.class);
		intent.putExtra("data",position);
		intent.putExtra("from", "save");
		startActivity(intent);
	}
	public void refresh(){
		DBHelper dbHelper = new DBHelper(this);
		data = dbHelper.getSaveInfo();
		dbHelper.close();
		savelist.setAdapter(adapter);
	}
}





